package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.uuid.Seq;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUserWork;
import com.ruoyi.system.mapper.SysUserWorkMapper;
import com.ruoyi.system.service.ISysUserWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysUserWorkServiceImpl implements ISysUserWorkService {

    @Autowired
    private SysUserWorkMapper sysUserWorkMapper;

    @Override
    public List<SysUserWork> selectUserWorkList(SysUserWork sysUserWork) {
        return sysUserWorkMapper.selectSysUserWorkList(sysUserWork);
    }

    @Override
    public SysUserWork selectSysUserWorkByWorkId(Long workId) {
        return sysUserWorkMapper.selectSysUserWorkByWorkId(workId);
    }

    @Override
    public Long insertWork(SysUserWork sysUserWork) {
        if (StringUtils.isNotNull(sysUserWork.getStartDate())& StringUtils.isNotNull(sysUserWork.getEndDate())){
            if (sysUserWork.getStartDate().after(sysUserWork.getEndDate())){
                throw new ServiceException("开始时间晚于结束时间，请重新选择时间");
            }
        }
        if (sysUserWork.getWorkId() != null) {
            sysUserWorkMapper.updateSysUserWork(sysUserWork);
        } else {
            sysUserWork.setWorkId(Seq.getSeqId());
            sysUserWork.setOrgId(SecurityUtils.getOrgId());
            sysUserWorkMapper.insertSysUserWork(sysUserWork);
        }
        return sysUserWork.getWorkId();
    }

    @Override
    public int deleteSysUserWorkByWorkIds(Long[] workIds) {
        return sysUserWorkMapper.deleteSysUserWorkByWorkIds(workIds);
    }
}
